//
//  HFStatusPictureView.swift
//  sinaMicroBlog
//
//  Created by mac on 15/11/18.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SDWebImage

private let pictureCellID = "pictureCellID"
private let pictureCellMargin: CGFloat = 5
class HFStatusPictureView: UICollectionView {
    var imageURLs: [NSURL]? {
        didSet {
            let viewSize = self.cacleViewSize()
            self.snp_updateConstraints { (make) -> Void in
                make.width.equalTo(viewSize.width)
                make.height.equalTo(viewSize.height)
            }
            testLabel.text = "\(imageURLs?.count ?? 0)"
        }
    }

    init() {
        let layout = UICollectionViewFlowLayout()
        //设置layout
        layout.minimumInteritemSpacing = pictureCellMargin
        layout.minimumLineSpacing = pictureCellMargin
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        registerClass(HFPictureViewCell.self, forCellWithReuseIdentifier: pictureCellID)
        dataSource = self
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func cacleViewSize() -> CGSize {
        
        let colum: CGFloat = 3.000000000001
        let maxWidth = screenW - 2 * StatusCellMargin
        let itemWidth = (maxWidth - 2 * pictureCellMargin) / colum
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //设置cell大小
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        let imageCount = imageURLs?.count ?? 0
        if imageCount == 0 {
            return CGSizeZero
        }
        
        if imageCount == 1 {
//            let imagesize = CGSizeMake(180, 120)
            let key = imageURLs?.last?.absoluteString
            let image = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(key!)
            let imageSize = image!.size
            layout.itemSize = imageSize
            return imageSize
        }
        
        if imageCount == 4 {
            let w = itemWidth * 2 + pictureCellMargin
            let imageSize = CGSizeMake(w, w)
            return imageSize
        }
        
        let row = CGFloat((imageCount - 1) / 3 + 1)
        let w = maxWidth
        let h = row * itemWidth + (row - 1) * pictureCellMargin
        return CGSizeMake(w, h)
        
    }
    
    private func setupUI() {
        backgroundColor = UIColor.randomColor()
        addSubview(testLabel)
        
        //约束
        testLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
    }
    //懒加载
    private lazy var testLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(25)
        label.textColor = UIColor.redColor()
        label.sizeToFit()
        
        return label
    }()

}

extension HFStatusPictureView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = dequeueReusableCellWithReuseIdentifier(pictureCellID, forIndexPath: indexPath) as! HFPictureViewCell
        cell.url = imageURLs![indexPath.item]
        return cell
    }
}

class HFPictureViewCell: UICollectionViewCell {
    
    //数据属性
    var url: NSURL? {
        didSet {
            imageView.sd_setImageWithURL(url, placeholderImage: nil)
        }
    }
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置UI
    private func setupUI() {
        addSubview(imageView)
        //约束
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    
    //懒加载
    private lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .ScaleAspectFill
        imageView.clipsToBounds = true
        
        return imageView
    }()
}





